﻿<?php
	
	require_once('config.php');
	
	/*
	 gay shit mysql wrapper
	 */
	class DB {
		
		//its a singleton
		protected static $instance = null;		
		//mysql connection holder
		protected $link = null;
		//query holding str
		protected $qry = "";

		function DB() {
			if(!$instance) {
				//get config from global scope
				global $config;
				//thats too fucking obvious to comment
				$this->link = mysql_connect(
					$config['db']['host'], 
					$config['db']['user'], 
					$config['db']['pass']
				) or die('Cant connect to mysql database');

				mysql_select_db(
					$config['db']['dbase'], 
					$this->link
				) or die('Cant select db');
				
				$instance = $this;
			} else {
				die("WTF ARE YOU DOING FUCKER!? 'DB' IS A SINGLETON!");
			}
		}
		
		function query($type = MYSQL_ASSOC, $querystr = null) {
			if($querystr == null) { 
				$querystr = $this->qry;
				$this->qry = '';
			}
			$res = mysql_query($querystr);

			if(!$res) {
				//haha fucking looser
				die('MySql query failed:'.mysql_error());
			}
			
			if(is_bool($res)) {
				return $res;
			}

			//fetch associative array
			$results = array();
			while($row = mysql_fetch_array($res, $type)) {
				array_push($results, $row);
			}
			
			return $results;
		}

		function update($table, $fields, $where = null) {
			$query = "UPDATE $table SET ";
			
			///////////////////
			//UPDATE $table SET ***
			foreach($fields as $field => $value) {
				$query .= "$field = $value,";
			}
			$query = substr($query,0,-1).' '; 
			
			///////////////////
			//WHERE ***
			if($where) {
				$query .= "WHERE";

				foreach($where as $field => $value) {
					$query .= " $field $value ";
				}	
				//erase last ','
				$query = substr($query,0,-1).' '; 
			}

			//done
			$this->qry .= $query;
		}

		function select($table, $fields = null, $where = null, $order = null) {
			$query = "SELECT ";
			
			///////////////////
			//SELECT ***
			if($fields) {
				$query .= join(', ', $fields);
			} else {
				$query .= ' * ';
			}
			
			///////////////////
			//FROM ***
			$query .= " FROM $table ";
			
			///////////////////
			//WHERE ***
			if($where) {
				$query .= "WHERE";

				foreach($where as $field => $value) {
					$query .= " $field $value ";
				}	
				//erase last ','
				$query = substr($query,0,-1).' '; 
			}

			//////////////////
			//ORDER BY ***
			if($order) {
				$query .= "ORDER BY $order";
			}

			//done
			$this->qry .= $query;
		}
		
		function insert($table, $values) {
			//INSERT INTO ***
			$query = "INSERT INTO $table ";
			
			//(***) VALUES(***)
			//array_keys(values) - getting field array
			$query .= '('.join(', ',array_keys($values)).')';
			
			$query .= ' VALUES(';
			foreach($values as $key) {
				if(is_numeric($key))
					$query .= $key.',';
				else
					$query .= "'$key',";
			}
			//erase last ','
			$query = substr($query,0,-1).')'; 

			//done
			$this->qry .= $query;
		}

		function union() {
			$this->qry .= ' UNION ';
		}

	}
	
	$db = new DB();
?>
